package com.jimmy.medical.entity;

import javax.persistence.*;

import com.fasterxml.jackson.annotation.JsonBackReference;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import java.util.Set;
import java.util.TreeSet;

/**
 * 充值记录
 * 
 * @author jimmy
 * @since 0.1
 */
@Data
@Entity
@Table(name = "recharge_record")
public class RechargeRecord extends AbstractEntity {
	/** 充值会员*/
	@ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "member_id")
    @NotFound(action = NotFoundAction.IGNORE)
    private Member member;
    /** 关联合同*/
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "contract_id")
    @NotFound(action = NotFoundAction.IGNORE)
	private Contract contract;
	/** 充值类型,年费/普通续次/卡*/
	@Column(name = "recharge_type", length = 10)
	private String rechargeType;

	/** 收费金额*/
	@Column(name = "money")
	private float money;

	/** 说明*/
	@Column(name = "description", length = 50)
	private String description;

	/** 是否开票*/
	@Column(name = "is_billing", length = 10)
	private String isBilling;
	
	/** 是否已开票*/
	@Column(name = "billed", length = 10)
	private String billed;
	
	/** 付款方式,1:现金;2:转账;3:微信支付*/
	@Column(name = "payment_method", length = 10)
	private String paymentMethod;
	
	/** 已付,1:是;0:否*/
	@Column(name = "is_payed", length = 10)
	private String isPayed;

	/** 发票信息*/
	@Column(name = "billing_info", length = 100)
	private String billingInfo;
	
	/** 入会时间*/
	@Column(name = "begin_time", length = 20)
	private String beginTime;
	
	/** 会员周期*/
	@Column(name = "cycle")
	private Integer cycle;

	/** 到期时间*/
	@Column(name = "expire_time", length = 20)
	private String expireTime;
	
	/** 推荐人*/
	@Column(name = "recommend_by", length = 20)
	private String recommendBy;

	/** 收款人*/
	@Column(name = "payee", length = 20)
	private String payee;
	
	/** 审核人*/
	@Column(name = "approve_by", length = 20)
	private String approveBy;

	/** 付款状态,1:未付款;2:已付款;3:财务已审核*/
	@Column(name = "status", length = 10)
	private String status;

	/** 卡状态,10:未开卡;2:正常;3:已挂失*/
	@Column(name = "card_status", length = 10)
	private String cardStatus;
	
	@Column(name = "card_no", length = 50)
	private String cardNo;
	
	@Column(name = "card_type", length = 2)
	private String cardType;
	
	@Column(name = "passwd", length = 50)
	private String password;
	
	@Column(name = "create_time", length = 20)
	private String createTime;
	@Column(name = "create_user", length = 50)
	private String createUser;
	@Column(name = "update_time")
	private String updateTime;
	@Column(name = "update_user", length = 20)
	private String updateUser;

	/** 支付流水号*/
	@Column(name = "pay_flow_no", length = 20)
	private String payFlowNo;

	/** 销售人*/
	@JsonBackReference
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "sale_user_id")
	private User saleUser;

	/** 服务项目*/
	@OneToMany(cascade = {CascadeType.REMOVE}, fetch=FetchType.LAZY, mappedBy="record")
	private Set<RechargeRecordServiceItem> recordItems = new TreeSet<RechargeRecordServiceItem>();

	/**
	 * 显示的名称
	 */
	@Transient
	private String name;

	public String getName() {
		if("1".equals(rechargeType)) {
			if(contract != null && contract.getMemberType() != null) {
				return contract.getMemberType().getName();
			}
		} else if("2".equals(rechargeType)) {
			if(recordItems != null && recordItems.size() > 0) {
				RechargeRecordServiceItem[] itemArray = recordItems.toArray(new RechargeRecordServiceItem[]{});
				RechargeRecordServiceItem item = itemArray[0];
				if(item != null && item.getItem() != null) {
					return item.getItem().getName();
				}
			}
		}
		return "";
	}
}
